package cn.jtfadmin.core.modules.sys.controller;



import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.dict.service.DictEntityService;
import cn.jtfadmin.base.lang.common.domain.dto.PageRDTO;
import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import cn.jtfadmin.base.lang.common.domain.dto.SpecPDTO;
import cn.jtfadmin.base.lang.common.domain.dto.SpecPagePDTO;
import cn.jtfadmin.base.lang.common.security.UserDetails;
import cn.jtfadmin.base.lang.common.service.BaseService;
import cn.jtfadmin.base.lang.config.user.UserHolder;
import cn.jtfadmin.base.shiro.statuscode.NoAuthorityStatusCodeConfig;
import cn.jtfadmin.base.lang.config.excel.ExcelWeb;
import cn.jtfadmin.core.config.shiro.UserDetailsPropertiesKey;
import cn.jtfadmin.core.modules.sys.domain.dto.*;
import cn.jtfadmin.core.modules.sys.domain.entity.SysUserEntity;
import cn.jtfadmin.core.modules.sys.service.SysUserActionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;


/**
 * 后管-系统用户管理
 *
 * @author jtf
 * @since 1.0
 */
@Slf4j
@RestController
@AllArgsConstructor
public class SysUserController {

    private BaseService<SysUserEntity> service;

    private DictEntityService<SysUserEntity> dictEntityService;

    private SysUserActionService sysUserService;



    /**
     * 系统获取当前用户信息
     *
     * @return 当前用户信息
     */
    @GetMapping(value = "/sys/user/getCurrent")
    public ResultDTO<SysUserModelDTO> getCurrentUser() {
        UserDetails userDetails = UserHolder.getUserDetails();
        SysUserModelDTO userModelDTO = (SysUserModelDTO) userDetails.getProperties().get(UserDetailsPropertiesKey.USER_MODEL_TYPE);
        return ResultDTO.successData(userModelDTO);

    }



    /**
     * 获取当前用户权限
     *
     * @return
     */
    @RequestMapping(value = "/sys/permission/getUserPermission", method = RequestMethod.GET)
    public ResultDTO<UserMenuRDTO> getUserPermissionByToken() {
        UserDetails userDetails = UserHolder.getUserDetails();
        UserMenuRDTO userMenuRDTO = (UserMenuRDTO) userDetails.getProperties().get(UserDetailsPropertiesKey.MENU_PERMISSION_TYPE);
        return ResultDTO.successData(userMenuRDTO);
    }

    /**
     * 刷新用户权限
     *
     * @return
     */
    @PostMapping(value = "/sys/user/refreshUserInfo")
    public ResultDTO<String> refreshUserInfo() {
        UserHolder.refreshCurrentUser();
        return ResultDTO.success();

    }




    /**
     * 管理端重置用户密码
     *
     * @param username 用户名
     * @param password 密码
     */
    @PutMapping(value = "/sys/user/changPassword")
    public ResultDTO<String> changePassword(@NotBlank String username, @NotBlank String password) {
        sysUserService.resetPassword(username, password);
        return ResultDTO.success();
    }


    /**
     * 管理端批量冻结用户
     *
     * @param sysUserFrozenPDTO 冻结参数
     */
    @PutMapping(value = "/sys/user/frozenBatch")
    public ResultDTO<String> frozenBatch(@Validated @RequestBody SysUserFrozenPDTO sysUserFrozenPDTO) {
        sysUserService.frozenBatch(sysUserFrozenPDTO);
        return ResultDTO.success();
    }


    /**
     * 用户修改当前密码
     *
     * @param username    用户名
     * @param password    密码
     * @param oldpassword 旧密码
     */
    @PutMapping(value = "/sys/user/updatePassword")
    public ResultDTO<String> updateUserPassword(String username, String password, String oldpassword) {
        sysUserService.updatePassword(username, password, oldpassword);
        return ResultDTO.success();
    }


    /**
     * 分页查询 系统用户
     *
     * @param specPagePDTO 查询条件
     * @return 分页数据
     */
    @GetMapping(value = "/sys/user/list")
    public ResultDTO<PageRDTO<SysUserModelDTO>> list(SpecPagePDTO<SysUserEntity> specPagePDTO) {
        return ResultDTO.successData(service.findModelAll(specPagePDTO, SysUserModelDTO.class));
    }

    /**
     * 按照ids查询 系统用户
     *
     * @param ids id,...
     * @return 分页数据
     */
    @GetMapping(value = "/sys/user/findByIds")
    public ResultDTO<List<SysUserModelDTO>> findByIds(String ids) {
        return ResultDTO.successData(service.findModelByIds(ids, SysUserModelDTO.class));
    }

    /**
     * 按照id查询 系统用户
     *
     * @param id id
     * @return 分页数据
     */
    @GetMapping(value = "/sys/user/findById")
    public ResultDTO<SysUserModelDTO> findById(String id) {
        return ResultDTO.successData(service.findModelRequiredById(id, SysUserModelDTO.class));
    }




    /**
     * 新增 系统用户
     *
     * @param addPDTO 新增参数
     * @return id
     */
    @PostMapping(value = "/sys/user/add")
    public ResultDTO<String> add(@RequestBody @Validated SysUserAddPDTO addPDTO) {
        return ResultDTO.successData(service.add(addPDTO).getId());
    }

    /**
     * 编辑 系统用户
     *
     * @param editPDTO 查询条件
     * @return 无
     */
    @PutMapping(value = "/sys/user/edit")
    public ResultDTO<String> edit(@RequestBody @Validated SysUserEditPDTO editPDTO) {
        return ResultDTO.successData(service.edit(editPDTO).getId());
    }

    /**
     * 按照id删除 系统用户
     *
     * @param id id
     * @return 无
     */
    @DeleteMapping(value = "/sys/user/del")
    public ResultDTO<String> del(@RequestParam() @NotBlank String id) {
        service.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 批量删除 系统用户
     *
     * @param ids 查询条件
     * @return 分页数据
     */
    @DeleteMapping(value = "/sys/user/delBatch")
    public ResultDTO<String> delBatch(@RequestParam() @NotBlank String ids) {
        service.deleteBatch(ids);
        return ResultDTO.success();
    }


    /**
     * 导入excel
     */
    @PostMapping("/sys/user/importExcel")
    public ResultDTO<String> importExcel(MultipartFile file) {
        try {
            service.importExcel(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultDTO.success();
    }

    /**
     * 导出excel
     */
    @GetMapping("/sys/user/exportExcel")
    public void exportExcel(SpecPDTO<SysUserEntity> specPDTO) {
        service.exportExcel(specPDTO, ExcelWeb.addCurrentResposeHeader("系统用户"));
    }


    /**
     * 获取用户字典
     * @param specPDTO
     * @param columns
     * @return
     */
    @GetMapping(value = "/sys/user/named")
    public ResultDTO<List<DictItemModelDTO>> named(SpecPDTO<SysUserEntity> specPDTO, String columns, String keyField){
        List<DictItemModelDTO> dictModel = dictEntityService.findDictModelListBySpec(columns, specPDTO, keyField);
        return ResultDTO.successData(dictModel);
    }
}
